package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.application.query.admin.ResourceQuery;
import com.spa.domain.entity.Resource;
import com.spa.infrastructure.mapper.ResourceMapStruct;
import com.spa.infrastructure.repository.mapper.ResourceMapper;
import com.spa.infrastructure.repository.po.ResourcePO;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Resource仓储层.
 *
 * @author liangjun
 */
@Component
@Slf4j
public class ResourceRepositoryService extends ServiceImpl<ResourceMapper, ResourcePO> {

    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 分页查询.
     *
     * @return 分页数据
     */
    public Page<ResourcePO> page(ResourceQuery query) {
        return page(Page.of(query.getCurrent(), query.getSize()));
    }

    /**
     * 列表查询.
     */
    public List<ResourcePO> queryResources(ResourceQuery query) {
        LambdaQueryWrapper<ResourcePO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollectionUtil.isNotEmpty(query.getIds()), ResourcePO::getId, query.getIds());
        wrapper.eq(ObjectUtils.isNotEmpty(query.getParentId()),ResourcePO::getParentId,query.getParentId());
        List<ResourcePO> resources = list(wrapper);
        return resources;
    }

    /**
     * 查询一个.
     */
    public Resource getOne() {
        LambdaQueryWrapper<ResourcePO> wrapper = Wrappers.lambdaQuery();
        ResourcePO resourcePo = getOne(wrapper);
        return ResourceMapStruct.INSTANCE.resourcePOToResource(resourcePo);
    }

    /**
     * 新增.
     */
    public void add(ResourcePO resourcePo) {
        resourceMapper.insert(resourcePo);
    }

    /**
     * 修改.
     */
    public void edit(ResourcePO resourcePo) {
        LambdaUpdateWrapper<ResourcePO> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ObjectUtil.isNotNull(resourcePo.getId()), ResourcePO::getId, resourcePo.getId());

        wrapper.set(StringUtils.isNotEmpty(resourcePo.getResourceName()),ResourcePO::getResourceName,resourcePo.getResourceName());
        wrapper.set(ObjectUtils.isNotEmpty(resourcePo.getParentId()),ResourcePO::getParentId,resourcePo.getParentId());
        wrapper.set(ObjectUtils.isNotEmpty(resourcePo.getResourceType()),ResourcePO::getResourceType,resourcePo.getResourceType());
        wrapper.set(StringUtils.isNotEmpty(resourcePo.getResourceUrl()),ResourcePO::getResourceUrl,resourcePo.getResourceUrl());
        wrapper.set(ObjectUtils.isNotEmpty(resourcePo.getState()),ResourcePO::getState,resourcePo.getState());
        wrapper.set(ObjectUtils.isNotEmpty(resourcePo.getResourceSort()),ResourcePO::getResourceSort,resourcePo.getResourceSort());
        wrapper.set(StringUtils.isNotEmpty(resourcePo.getIcon()),ResourcePO::getIcon,resourcePo.getIcon());

        resourceMapper.update(wrapper);
    }
}
